<?php


namespace App\Services\Auth;

use App\Common\Constant;
use App\Common\ResponseCode;
use App\Exceptions\BusinessException;
use App\Models\Customer\Customer;
use App\Services\BaseService;
use App\Services\Customer\CustomerService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;

/**
 * 授权相关业务类
 *
 * @author WangBin
 * @date 2021-09-24
 * @package App\Http\Services
 */
class AuthService extends BaseService
{
    /**
     * 校验手机号验证码登录
     *
     * @param string $phone
     * @param string $smsCode
     * @return
     * @throws
     */
    public function checkCustomerLoginByVCode(string $phone, string $smsCode)
    {
        // 校验验证码
        AuthService::getInstance()->checkCaptcha(Constant::CAPTCHA_SCENE_LOGIN, $phone, $captcha);
        // 获取客户ID
        return Customer::query()->where('phone', $phone)->where('deleted', 0)->first();
    }

    /**
     * 校验账户密码登录
     *
     * @param $username
     * @param $password
     * @return
     * @throws
     */
    public function checkCustomerLoginByPwd($username, $password)
    {
        // 校验用户名
        $customer = Customer::query()->where('username', $username)->where('deleted', 0)->first();
        if(is_null($customer)) {
            throw new BusinessException(ResponseCode::CUSTOMER_USERNAME_PWD_NOT_FOUND);
        }
        // 校验密码
        if(!Hash::check($password . $customer->secret, $customer->password)) {
            throw new BusinessException(ResponseCode::CUSTOMER_USERNAME_PWD_NOT_FOUND);
        }
        return $customer;
    }

    /**
     * 校验验证码
     *
     * @param $scene
     * @param $phone
     * @param $code
     * @return bool
     * @throws
     */
    public function checkCaptcha($scene, $phone, $code)
    {
        // 非生产环境不校验验证码
        if(!app()->environment('production')) {
            return true;
        }
        $key = $scene . '_captcha_' . $phone;
        $isPass = $code == Cache::get($key);
        if($isPass) {
            Cache::forget($key);
            return true;
        } else {
            throw new BusinessException(ResponseCode::PHONE_CODE_ERROR);
        }
    }

    /**
     * 重置客户密码
     *
     * @param $customerId
     * @param $oldPassword
     * @param $newPassword
     * @param $confirm
     * @throws BusinessException
     * @return
     */
    public function resetPwd($customerId, $oldPassword, $newPassword, $confirm)
    {
        // 校验参数
        if(empty($customerId) || empty($oldPassword) || empty($newPassword) || empty($confirm)) {
            throw new BusinessException(ResponseCode::PARAM_ILLEGAL);
        }
        // 确认密码校验
        if($newPassword != $confirm) {
            throw new BusinessException(ResponseCode::CUSTOMER_PWD_CONFIRM_FAIL);
        }

        // 获取客户
        $customer = CustomerService::getInstance()->getById($customerId);
        if(is_null($customer)) {
            throw new BusinessException(ResponseCode::CUSTOMER_NOT_FOUND);
        }
        // 校验原密码
        if(!Hash::check($oldPassword . $customer->secret, $oldPassword)) {
            throw new BusinessException(ResponseCode::CUSTOMER_PWD_ERROR);
        }
        // 更新客户密码
        $customer->password = Hash::make($newPassword . $customer->secret);
        return $customer->save();
    }

    /**
     * 用户退出登录
     *
     * @param $customerId
     * @return bool
     * @throws BusinessException
     */
    public function logout($customerId)
    {
        if(empty($customerId)) {
            throw new BusinessException(ResponseCode::OP_FAIL);
        }
        // 删除token
        return Cache::forget(Constant::JWT_PREFIX_CUSTOMER . $customerId);
    }
}
